package com.xavier.dong.nio.zerocopy;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

/**
 * 服务器端
 *
 * @author XavierDong
 **/
public class NewIOServer {


    public static void main(String[] args) {

        InetSocketAddress socketAddress = new InetSocketAddress(7001);

        try {
            ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();

            ServerSocket serverSocket = serverSocketChannel.socket();

            serverSocket.bind(socketAddress);

            // 创建Buffer
            ByteBuffer byteBuffer = ByteBuffer.allocate(4096);

            while (true) {
                SocketChannel socketChannel = serverSocketChannel.accept();

                int readCount = 0;

                while (readCount != -1) {
                    readCount = socketChannel.read(byteBuffer);
                }

                // 将 ByteBuffer 倒带
                // 将 position =0  mark 作废
                byteBuffer.rewind();

            }
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
